package service

import (
	"errors"
	"github.com/gccai/write_helper/model"
	"github.com/gccai/write_helper/repository"
	"github.com/gccai/write_helper/utils"
)

type DesignService struct {
	Repo repository.DesignRepositoryInterface
}

//  增加exist功能，我现在默认的是传过来的数据都是对的，但你要知道不是每一个前端都像你一样。
type DesignServiceInterface interface {
	// 根据某个 userid和bookname中的did将选中的card内容显示出来。
	GetCardContent(card model.Design) (*model.Design, error)
	// 显示所有的card的DesignTitle和DType
	GetAllCard(card model.Design) (*[]model.Design, error)
	// 显示 是书籍的还是某一章节的Card内容
	GetOneTypeCard(card model.Design) (*[]model.Design, error)
	// 增加一个Card
	Add(card model.Design) (bool, error)
	Edit(card model.Design) (*model.Design, error)
	Delete(card model.Design) (bool, error)
}

func (d DesignService) GetCardContent(card model.Design) (*model.Design, error) {
	if card.UserId == "" || card.BookName == "" || card.DId == "" {
		return nil, errors.New("args is nil")
	}
	UUID, err := utils.CardNum2UUID(card.DId)
	if err != nil {
		return nil, err
	}
	card.DId = UUID
	return d.Repo.Get(card)
}

func (d DesignService) GetAllCard(card model.Design) (*[]model.Design, error) {
	if card.UserId == "" || card.BookName == "" {
		return nil, errors.New("args is nil")
	}
	return d.Repo.GetAllCard(card.UserId, card.BookName)
}

func (d DesignService) GetOneTypeCard(card model.Design) (*[]model.Design, error) {
	if card.UserId == "" || card.BookName == "" || card.DesignAttribution == 0 {
		return nil, errors.New("args is nil")
	}
	return d.Repo.GetOneTypeCard(card.UserId, card.BookName, card.DesignAttribution)
}

func (d DesignService) Add(card model.Design) (bool, error) {
	//  根据userid,bookname,did看有没有did重复的，没有再进行操作。
	UUID, err := utils.CardNum2UUID(card.DId)
	if err != nil {
		return false, err
	}
	card.DId = UUID
	return d.Repo.Add(card)
}

func (d DesignService) Edit(card model.Design) (*model.Design, error) {
	UUID, err := utils.CardNum2UUID(card.DId)
	if err != nil {
		return nil, err
	}
	card.DId = UUID
	return d.Repo.Edit(card)
}

func (d DesignService) Delete(card model.Design) (bool, error) {
	UUID, err := utils.CardNum2UUID(card.DId)
	if err != nil {
		return false, err
	}
	card.DId = UUID
	return d.Repo.Delete(card.UserId, card.BookName, card.DId)
}
